package net.simflow.swf.analyzer.network.data;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;

public class Network {
	public static final String NETWORK = "Network";
	public static final String SERVICE_NETWORK = "Service Network";

	private String name;
	private HashMap<String, Flow> flowMap = new HashMap<String, Flow>();
	private HashSet<Node> nodeSet = new HashSet<Node>();
	private HashMap<String, Service> serviceMap = new HashMap<String, Service>();
	private HashSet<Instance> instanceSet = new HashSet<Instance>();

	public Network() {

	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void addFlow(Flow flow, String flowName) {
		this.flowMap.put(flowName, flow);
		flow.setNameInNetwork(flowName);
	}

	public boolean addNode(Node node) {
		return this.nodeSet.add(node);
	}

	public boolean addAllNode(Collection<? extends Node> nodeSet) {
		return this.nodeSet.addAll(nodeSet);
	}

	public void addService(Service service, String name) {
		this.serviceMap.put(name, service);
		service.setNameInNetwork(name);
	}

	public void addService(Service service) {
		this.serviceMap.put(service.getNameInNetwork(), service);
	}

	public boolean addInstance(Instance instance) {
		return this.instanceSet.add(instance);
	}

	public boolean addAllInstance(Collection<? extends Instance> instances) {
		return this.instanceSet.addAll(instances);
	}

	public HashMap<String, Flow> getFlowMap() {
		return flowMap;
	}

	public HashSet<Node> getNodeSet() {
		return nodeSet;
	}

	public HashMap<String, Service> getServiceMap() {
		return serviceMap;
	}

	public HashSet<Instance> getInstanceSet() {
		return instanceSet;
	}

	public String getShortInfo() {
		StringBuffer str = new StringBuffer();
		str.append(name);
		str.append("{");
		str.append("FLow(" + this.getFlowMap().size() + ");");
		str.append("Node(" + this.getNodeSet().size() + ");");
		str.append("Service(" + this.getServiceMap().size() + ")");
		str.append("}");

		return str.toString();
	}

	public String getFullInfo() {
		StringBuffer str = new StringBuffer();

		str.append(name);
		str.append("{");
		str.append("Flows(" + this.getFlowMap().size() + ")[");
		for (Flow f : this.getFlowMap().values()) {
			str.append(f.getFullInfo()).append(",");
		}
		str.deleteCharAt(str.length() - 1);
		str.append("]");
		str.append("Nodes(" + this.getNodeSet().size() + ")[");
		for (Node n : this.getNodeSet()) {
			str.append(n.getFullInfo()).append(",");
		}
		str.deleteCharAt(str.length() - 1);
		str.append("]");
		str.append("Services(" + this.getServiceMap().size() + ")[");
		for (Service s : this.getServiceMap().values()) {
			str.append(s.getFullInfo()).append(",");
		}
		str.deleteCharAt(str.length() - 1);
		str.append("]");
		str.append("}");

		return str.toString();
	}

	@Override
	public String toString() {
		return getShortInfo();
	}
}
